package com.ewanghuiju.app.ui.my.activity;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.BaseActivity;
import com.ewanghuiju.app.base.contract.my.MyContract;
import com.ewanghuiju.app.model.bean.local.TabEntity;
import com.ewanghuiju.app.model.bean.response.LbImgBean;
import com.ewanghuiju.app.model.bean.response.OrderStatusResponBean;
import com.ewanghuiju.app.model.bean.response.UserInfoResponBean;
import com.ewanghuiju.app.model.bean.response.VersionResponBean;
import com.ewanghuiju.app.model.http.response.BaseResponse;
import com.ewanghuiju.app.presenter.my.MyPresenter;
import com.ewanghuiju.app.ui.main.adapter.MyPagerAdapter;
import com.ewanghuiju.app.ui.my.fragment.ShouyiCenterFragment;
import com.ewanghuiju.app.util.StartActivityUtil;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.gyf.immersionbar.ImmersionBar;

import java.util.ArrayList;
import java.util.List;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author sj  qq:751943509
 * @class 收益中心页面
 * @time 2020/2/24
 */
public class ShouyiCenterActivity extends BaseActivity<MyPresenter> implements MyContract.View {
    @BindView(R.id.commonTabLayout)
    CommonTabLayout commonTabLayout;
    @BindView(R.id.view_top)
    View viewTop;
    @BindView(R.id.viewpage)
    ViewPager viewpage;
    @BindView(R.id.tv_banlance)
    TextView tvBanlance;
    @BindView(R.id.tv_have_withdrawal)
    TextView tvHaveWithdrawal;
    @BindView(R.id.tv_last_js)
    TextView tvLastJs;
    @BindView(R.id.tv_total_sy)
    TextView tvTotalSy;


    private ArrayList<CustomTabEntity> mTabEntities;
    private List<String> titleList;

    private List<Fragment> fragmentList;

    @Override
    protected int getLayout() {
        return R.layout.activity_shouyi_center;
    }

    @Override
    protected void initEventAndData() {

        mToolbar.setBackground(null);
        mToolbar.setNavigationIcon(R.mipmap.icon_fan_white);
        tvTitle.setTextColor(ContextCompat.getColor(mContext, R.color.white));
        GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                new int[]{Color.parseColor("#DA291C"), Color.parseColor("#FFFFFF")});
        viewTop.setBackground(gradientDrawable);

        initCommonTabLayoutAndFragment();
    }

    private void initCommonTabLayoutAndFragment() {

        fragmentList = new ArrayList<>();
        mTabEntities = new ArrayList<>();
        titleList = new ArrayList<>();
        titleList.add("今日收益");
        titleList.add("昨日收益");
        titleList.add("本月收益");
        titleList.add("上月收益");
        mTabEntities.add(new TabEntity("今日收益", R.mipmap.icon_selected_tab, 0));
        mTabEntities.add(new TabEntity("昨日收益", R.mipmap.icon_selected_tab, 0));
        mTabEntities.add(new TabEntity("本月收益", R.mipmap.icon_selected_tab, 0));
        mTabEntities.add(new TabEntity("上月收益", R.mipmap.icon_selected_tab, 0));

        for (int i = 0; i < titleList.size(); i++) {
            ShouyiCenterFragment shouyiCenterFragment = new ShouyiCenterFragment();
            Bundle bundle = new Bundle();
            bundle.putInt(Constants.SHOUYI_TYPE, i + 1);
            shouyiCenterFragment.setArguments(bundle);
            fragmentList.add(shouyiCenterFragment);
        }

        viewpage.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), 0, fragmentList, titleList));
        commonTabLayout.setTabData(mTabEntities);

        commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewpage.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });

        viewpage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                commonTabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        viewpage.setCurrentItem(0);

        mPresenter.getData();
    }

    @OnClick({R.id.tv_to_cash})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {    //判断是否多次点击
            return;
        }
        switch (view.getId()) {
            case R.id.tv_to_cash:
                //去提现
                new StartActivityUtil(mContext, WalletCashActivity.class).startActivity(true);
                break;
            default:
                break;
        }
    }

    @Override
    protected int getTitleIntText() {
        return R.string.shouyi_center_title;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .titleBar(mToolbar, true)
                .init();
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void showContent(String list) {

    }

    @Override
    public void showUserInfo(UserInfoResponBean bean) {
        //获取到用户信息
        if (bean != null) {
            tvBanlance.setText(TextUtils.isEmpty(bean.getBalance()) ? "0.00" : bean.getBalance());
        }
    }

    @Override
    public void showUserInfoError() {

    }

    @Override
    public void settingAvaterSuccess() {

    }

    @Override
    public void bindWxSuccess() {

    }

    @Override
    public void unBindWxSuccess() {

    }

    @Override
    public void showOsstoken(BaseResponse baseResponse) {

    }

    @Override
    public void showLbImg(int type, List<LbImgBean> list) {

    }

    @Override
    public void showUpgradeInfo(VersionResponBean versionResponBean) {

    }

    @Override
    public void showPtiorderStatus(OrderStatusResponBean orderStatusResponBean) {

    }

    /**
     * 顶部信息展示
     * @param withdrawal
     * @param lastJs
     * @param totalSy
     */
    public void setTextInfo(String withdrawal, String lastJs, String totalSy) {

        if (tvHaveWithdrawal != null) {
            tvHaveWithdrawal.setText(withdrawal);
        }
        if (tvLastJs != null) {
            tvLastJs.setText(lastJs);
        }
        if (tvTotalSy != null) {
            tvTotalSy.setText(totalSy);
        }
    }
}
